<template>
  <div v-loading="delLoading">
    <Search :search="search" :reset="reset">
      <template v-slot:body>
        <el-form-item label="盘点方案编码" prop="plan_no">
          <el-input v-model="query.plan_no" name="plan_no" clearable />
        </el-form-item>
        <el-form-item label="盘点方案名称" prop="plan_name">
          <el-input v-model="query.plan_name" name="plan_name" clearable />
        </el-form-item>
        <el-form-item label="单据状态" prop="statusArr">
          <el-select v-model="query.statusArr" multiple placeholder="选择单据状态" style="width: 240px" clearable>
            <el-option v-for="item in InventoryPlanStatusArr" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="库存组织" prop="org_code">
          <el-input v-model="query.org_code" name="org_code" clearable />
        </el-form-item>
      </template>
    </Search>
    <div class="table-default">
      <Operate :show="open" />
      <el-table :data="tableData" class="mt-3" v-loading="loading" border>
        <el-table-column prop="plan_no" label="盘点方案编码" width="200" />
        <el-table-column prop="plan_name" label="盘点方案名称" width="180" />
        <el-table-column prop="status" label="状态" width="110">
          <template #default="scope">
            <el-tag class="ml-2" type="primary" v-if="scope.row.status == 0">{{ InventoryPlanStatus[scope.row.status] }}</el-tag>
            <el-tag class="ml-2" type="success" v-if="scope.row.status == 1">{{ InventoryPlanStatus[scope.row.status] }}</el-tag>
            <el-tag class="ml-2" type="warning" v-if="scope.row.status == 2">{{ InventoryPlanStatus[scope.row.status] }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="wms_code" label="仓库代码" width="100" />
        <el-table-column prop="org_code" label="库存组织" width="100" />
        <el-table-column prop="remark" label="备注" />
        <el-table-column prop="split_order_rules" label="分单规则" width="120">
          <template #default="scope">
            <el-tag class="ml-2" type="primary">{{ SplitOrderRules[scope.row.split_order_rules] }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="default_qty" label="实盘数默认值" width="130">
          <template #default="scope">
            {{ Number(scope.row.default_qty) }}
          </template>
        </el-table-column>
        <el-table-column prop="inventory_eadline" label="库存截止时间" width="120" />
        <el-table-column prop="wms_range" label="盘点范围" width="120" />
        <el-table-column prop="created_at" label="创建时间" width="150" />

        <el-table-column label="操作" width="300" fixed="right">
          <template #default="scope">
            <Show @click="openHandler(api, scope.row.id)" />
            <Update @click="open(scope.row.id)" v-if="scope.row.status == '0'" />
            <el-button type="warning" size="small" @click="approveHandler(scope.row.id)" v-if="scope.row.status == '0'" v-loading.fullscreen.lock="fullscreenLoading">审核</el-button>
            <el-button type="warning" size="small" @click="approveJDHandler(scope.row.id)" v-if="scope.row.status == '1'" v-loading.fullscreen.lock="fullscreenLoading">金蝶推送</el-button>
            <Destroy @click="destroy(api, scope.row.id)" v-if="scope.row.status == '0'" />
          </template>
        </el-table-column>
      </el-table>
      <Paginate />
    </div>

    <Dialog v-model="visible" :title="title" destroy-on-close style="height: 75%; overflow: hidden">
      <Create @close="closeSelf" :primary="id" :api="api" />
    </Dialog>
    <Dialog v-model="visibleShow" :title="'查看'" destroy-on-close style="height: 70%; overflow: auto; width: 40%">
      <showVue @close="closeSelf" :primary="showId" :api="showApi" />
    </Dialog>
  </div>
</template>

<script lang="ts" setup>
import { computed, onMounted, ref, reactive,watch } from 'vue'
import Create from './create.vue'
import { useGetList } from '/admin/composables/curd/useGetList'
import { useDestroy } from '/admin/composables/curd/useDestroy'
import { useOpen } from '/admin/composables/curd/useOpen'
import http from '/admin/support/http'
import { ElMessage } from 'element-plus'
import showVue from './show.vue'
import { setTableHeight } from '/admin/utils/setHeight.js'

const api = 'inventory/plan'

const { data, query, search, reset, loading } = useGetList(api)
const { destroy, deleted,delLoading } = useDestroy()
const { open, close, title, visible, id } = useOpen()

const tableData = computed(() => data.value?.data)

const fullscreenLoading = ref(false)
let visibleShow = ref(false)
let showId = ref('')
let showApi = ref('')
let openHandler = (api, id) => {
  visibleShow.value = true
  showApi.value = api
  showId.value = id
}

const approveHandler = id => {
  fullscreenLoading.value = true
  http
    .post(`inventory/plan/confirm/${id}`)
    .then(r => {
      fullscreenLoading.value = false
      ElMessage({
        message: r.data.message,
        type: 'success',
      })
      search()
    })
    .catch(error => {
      fullscreenLoading.value = false
    })
}
const approveJDHandler = id => {
  fullscreenLoading.value = true
  http
    .post(`inventory/plan/verify/${id}`)
    .then(r => {
      fullscreenLoading.value = false
      ElMessage({
        message: r.data.message,
        type: 'success',
      })
      search()
    })
    .catch(error => {
      fullscreenLoading.value = false
    })
}
let InventoryPlanStatus = reactive({})
let InventoryPlanStatusArr = ref([])
let SplitOrderRules = reactive({})

// 盘点方案状态
const getInventoryPlanStatus = () => {
  http.get(`options/InventoryPlanStatus`).then(r => {
    console.log('盘点方案状态', r.data)
    InventoryPlanStatusArr.value = r.data.data
    InventoryPlanStatus = r.data.data.reduce((result, item) => {
      result[item.value] = item.label
      return result
    }, {})
    console.log('盘点方案状态', InventoryPlanStatus)
  })
  http.get(`options/SplitOrderRules`).then(r => {
    console.log('拆单规则状态', r.data)
    SplitOrderRules = r.data.data.reduce((result, item) => {
      result[item.value] = item.label
      return result
    }, {})
    console.log('拆单规则状态', SplitOrderRules)
  })
}
getInventoryPlanStatus()

const closeSelf = () => {
  visible.value = false
  visibleShow.value = false
  search()
}

watch(
  () => query.value.statusArr,
  (newValue, oldValue) => {
    console.log('监听状态数组', newValue)
    query.value.status = newValue.toString()
  },
)

onMounted(() => {
  search()
  deleted(reset)
  setTableHeight('.table-default')
})
</script>

<style lang="css">
.el-dialog__body {
  padding: 0;
  /* overflow: hidden; */
}
.el-overlay-dialog {
  top: -55px !important;
  bottom: 30px !important;
  /* overflow: auto; */
}
.el-button--small {
  margin-left: 12px;
}
.el-table {
  height: 80%;
}
</style>